<?php
class sms_model_reciever extends Model {
	public function __construct(){
		parent::__construct();
		$this->table = 'app_sms_session';
	}
	
	/**
	 * 根据消息id 获取参与会话的列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $self
	 */
	public  function getReciverByUid($sms_id,$self=''){
		if ($self) {
			$sql = 'SELECT sms_uid,sms_nickname FROM '.$this->table. 'WHERE sms_id=&sms_id AND sms_uid!='.$self;
		}
		else $sql = 'SELECT sms_uid,sms_nickname FROM '.$this->table. 'WHERE sms_id=&sms_id ';
		$para = array('sms_id'=>$sms_id);
		return $this->db->fetchAll($sql, $params);
	}
		
	public function add($arguments){
		$arguments = filter_params($arguments, 'sms_id', 'uid', 'username', 'unread', 'type', 'is_deleted');
		trim_params($arguments);
		$arguments['is_refused'] = $arguments['is_deleted']?1:0;
		$arguments['is_deleted'] = $arguments['is_deleted']?1:0;
		$arguments['is_read'] = 0;
		
		$flag = $this->db->insert($this->table, $arguments);
		return $flag;
	}
	
	public function updateRead($sms_id, $uid, $is_read){
		$sql = "update ".$this->table." set is_read=&is_read where sms_id=&sms_id and uid=&uid";
		$params = array(
			'is_read'	=> 	$is_read,
			'sms_id' 	=> 	$sms_id,
			'uid'		=>	$uid
		);
		return $this->db->query($sql, $params);
	}
	
	public function setRefuse($sms_id, $uid){
		$sql = "update ".$this->table." set is_refused=1 where sms_id=&sms_id and uid=&uid";
		$params = array(
			'sms_id' 	=> 	$sms_id,
			'uid'		=>	$uid
		);
		return $this->db->query($sql, $params);
	}
	
	public function setDelete($sms_id, $uid){
		$sql = "update ".$this->table." set is_deleted=1 where sms_id=&sms_id and uid=&uid";
		$params = array(
			'sms_id' 	=> 	$sms_id,
			'uid'		=>	$uid
		);
		return $this->db->query($sql, $params);
	}
	
	public function getRecievers($sms_id){
		$sql = "select uid, username from ".$this->table." where sms_id=&sms_id";
		$params = array('sms_id'=>$sms_id);
		return $this->db->fetchAll($sql, $params);
	}
	
	public function select($sms_id){
		$sql = "select * from ".$this->table." where sms_id=&sms_id";
		$params = array('sms_id'=>$sms_id);
		return $this->db->fetchAll($sql, $params);
	}
	
	public function check($sms_id, $uid){
		$sql = "select * from ".$this->table." where sms_id=&sms_id and uid=&uid";
		$params = array(
			'sms_id'=>$sms_id,
			'uid'=>$uid
		);
		return $this->db->fetchRow($sql, $params)!=null;
	}
	
	public function findFirstReciever($sms_id, $exclusive){
		$sql = "select * from ".$this->table." where sms_id=&sms_id";
		$params = array(
			'sms_id' 	=> $sms_id,
		);
		$result = $this->db->query($sql, $params);
		while($row = mysql_fetch_assoc($result)){
			if($row['uid']!=$exclusive){
				return $row;
			}
		}
	}
	
	public function rollback($sms_id){
		$sql = "delete from ".$this->table." where sms_id=&sms_id";
		$params = array('sms_id'=>$sms_id);
		return $this->db->query($sql, $params);
	}
	
	public function addUnread($sms_id, $users){
		if(count($users)<1) return;
		$sql = "update ".$this->table." set unread=unread+1 where sms_id=&sms_id and uid in (".implode(",", $users).")";
		$params = array(
			'sms_id'=>$sms_id
		);
		return $this->db->query($sql, $params);
	}
	
	public function resetRead($uid, $type){
		$sql = "update ".$this->table." set unread=0 where uid=$uid and type=$type";
		$this->db->query($sql);
		return $this->db->affectCount();
	}
	
	/*public function getUnread($uid, $type){
		$sql = "select sum(unread) unread from ".$this->table." where uid=$uid and type=$type and is_deleted=0 and is_refused=0";
		return $this->db->fetchField($sql, 'unread');
	}*/
	
	/*public function getUnreadAll($uid){
		$sql = "select sum(unread) unread from ".$this->table." where uid=$uid and is_deleted=0 and is_refused=0";
		return $this->db->fetchField($sql, 'unread');
	}*/
	
	public function refuse($uid, $sms_id,$delAll = false){
		/**
		 * 如果是全部删除
		 */
		if($delAll===true){
			$sql = "update ".$this->table." set is_refused=1 where uid=&uid";
			$params = array(
			'uid'=>$uid
			);
			return $this->db->query($sql, $params);
		}
		if(is_array($sms_id) && !empty($sms_id)){
			$sms_id = '('.implode(',',$sms_id).')';
		}
		else $sms_id = '('.$sms_id.')';
		$sql = "update ".$this->table." set is_refused=1 where uid=&uid and sms_id IN $sms_id ";
		$params = array(
			'uid'=>$uid
		);
		return $this->db->query($sql, $params);
	}
	
	public function updateLastMessage($msg_id, $sms_id, $users, $self=0){
		if($self==0){
			if(count($users)<1) return;
			$sql = "update ".$this->table." set last_msg_id=&msg_id where sms_id=&sms_id and uid in (".implode(",", $users).")";
		}else{
			$sql = "update ".$this->table." set last_msg_id=&msg_id where sms_id=&sms_id limit 1";
		}
		$params = array(
			'msg_id'=>$msg_id,
			'sms_id'=>$sms_id,
			'uid'=>$uid
		);
		return $this->db->query($sql, $params);
	}
	
	public function updateLastSend($msg_id, $sms_id, $uid){
		$sql = "update ".$this->table." set last_send_id=&msg_id where sms_id=&sms_id and uid=&uid";
		$params = array(
			'msg_id'=>$msg_id,
			'sms_id'=>$sms_id,
			'uid'=>$uid
		);
		return $this->db->query($sql, $params);
	}
	
	/*public function getUnreadAppRequest($uid, $name){
		$sql = "select sum(unread) unread from ".$this->table." r
		inner join app_sms s on r.sms_id = s.id
		where r.uid=$uid and r.type=0 and r.is_deleted=0 and r.is_refused=0 and s.username='[:".$name.":]'";
		$params = array('uid'=>$uid);
		return $this->db->fetchField($sql, "unread", $params);
	}*/
}